package elitech.vietnam.myfashion.dialog;

import java.io.IOException;
import java.util.ArrayList;

import org.xmlpull.v1.XmlPullParserException;

import android.app.Dialog;
import android.app.ProgressDialog;
import android.view.View;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
import elitech.vietnam.myfashion.MainActivity;
import elitech.vietnam.myfashion.R;
import elitech.vietnam.myfashion.doinbackground.CallServiceTask;
import elitech.vietnam.myfashion.entites.City;
import elitech.vietnam.myfashion.entites.District;
import elitech.vietnam.myfashion.extend.AsyncTask;
import elitech.vietnam.myfashion.wsclient.Service;

public class EditShippingDialog extends Dialog implements
		android.view.View.OnClickListener {

	private MainActivity mContext;
	private Dialog mDialog;

	private Button mBtnContinue;
	private EditText mTxtAddress, mTxtPhone;
	private Spinner mSpinCity, mSpinDistrict;
	private ArrayList<String> mCityListString = new ArrayList<String>();
	private ArrayList<String> mDistrictListString = new ArrayList<String>();
	private ArrayList<District> mDistrictListTemp = new ArrayList<District>();
	private ArrayAdapter<String> mCityAdapter, mDistrictAdapter;
	private int mCityLastPost, mDistrictLastPost;

	public EditShippingDialog(MainActivity context) {
		super(context, R.style.Dialog);
		this.mDialog = this;
		this.mContext = context;
		getWindow().setSoftInputMode(
				WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

		setContentView(R.layout.editshipping);
		mTxtAddress = (EditText) findViewById(R.id.ship_txtAddress);
		mSpinCity = (Spinner) findViewById(R.id.ship_spinnerCity);
		mSpinDistrict = (Spinner) findViewById(R.id.ship_spinnerDistrict);
		mTxtPhone = (EditText) findViewById(R.id.ship_txtPhone);

		mBtnContinue = (Button) findViewById(R.id.ship_btnContinue);

		mCityAdapter = new ArrayAdapter<String>(mContext,
				android.R.layout.simple_spinner_item, mCityListString);
		mCityAdapter
				.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
		mSpinCity.setAdapter(mCityAdapter);

		mDistrictAdapter = new ArrayAdapter<String>(mContext,
				android.R.layout.simple_spinner_item, mDistrictListString);
		mDistrictAdapter
				.setDropDownViewResource(android.R.layout.simple_list_item_single_choice);
		mSpinDistrict.setAdapter(mDistrictAdapter);

		mTxtAddress.setText(mContext.mUser.Address);
		mTxtPhone.setText(mContext.mUser.Phone);
		mCityLastPost = mContext.mUser.City;
		mDistrictLastPost = mContext.mUser.District;

		if (mContext.mListDistrict.size() == 0) {
			GetListDistrict();
		}

		if (mContext.mListCity.size() == 0) {
			GetListCity();
		} else {
			ListCityToListString();
		}

		mBtnContinue.setOnClickListener(this);
		mSpinCity.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1, int pos,
					long arg3) {
				ListDistrictToListString(mContext.mListCity.get(pos).Id);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
			}
		});

	}

	private void ListCityToListString() {
		for (City city : mContext.mListCity) {
			mCityListString.add(city.Name);
		}
		mCityAdapter.notifyDataSetChanged();
		if (mCityLastPost < mCityListString.size())
			mSpinCity.setSelection(mCityLastPost);
	}

	private void ListDistrictToListString(int IdCity) {
		mDistrictListString.clear();
		mDistrictListTemp.clear();
		for (District district : mContext.mListDistrict) {
			if (district.Publish == IdCity) {
				mDistrictListString.add(district.Name);
				mDistrictListTemp.add(district);
			}
		}
		mDistrictAdapter.notifyDataSetChanged();
		if (IdCity == mContext.mListCity.get(mCityLastPost).Id
				&& mDistrictLastPost < mCityListString.size())
			mSpinDistrict.setSelection(mDistrictLastPost);
	}

	public void GetListCity() {
		new CallServiceTask() {
			protected void onPreExecute() {
				mContext.mLoadingDialog.show();
			};

			private ArrayList<City> rs = new ArrayList<City>();

			public void work() {
				try {
					rs = service.getListCity();
				} catch (IOException e) {
				} catch (XmlPullParserException e) {
				}
			};

			protected void onPostExecute(Void result) {
				mContext.mListCity.addAll(rs);
				ListCityToListString();
				mContext.mLoadingDialog.dismiss();
			};
		}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
		;
	}

	public void GetListDistrict() {
		new CallServiceTask() {
			private ArrayList<District> rs = new ArrayList<District>();

			public void work() {
				try {
					rs = service.getListDistrict();
				} catch (IOException e) {
				} catch (XmlPullParserException e) {
				}
			};

			protected void onPostExecute(Void result) {
				mContext.mListDistrict.addAll(rs);
			};
		}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
	}

	@Override
	public void onClick(View v) {
		if (mTxtAddress.getText().toString().trim().length() == 0 || mTxtAddress.getText().toString().trim().length() == 0)
			Toast.makeText(mContext, R.string.shippingwarn, Toast.LENGTH_SHORT).show();
		else {
			mContext.mUser.Address = mTxtAddress.getText().toString();
			mContext.mUser.City = mSpinCity.getSelectedItemPosition();
			mContext.mUser.District = mSpinDistrict.getSelectedItemPosition();
			mContext.mUser.Phone = mTxtPhone.getText().toString();
			new CallServiceTask() {
				ProgressDialog mPDialog = new ProgressDialog(mContext, R.style.Dialog);
				int rs = -2;
				protected void onPreExecute() {
					mPDialog.show();
				};
				public void work() {
					try {
						rs = new Service().updateMemberShippingInfo(mContext.mUser.Id, mContext.mUser.Address, mContext.mUser.City, mContext.mUser.District, mContext.mUser.Phone);
					} catch (IOException e) {
					} catch (XmlPullParserException e) {
					}
				};
				protected void onPostExecute(Void result) {
					mPDialog.dismiss();
					if (rs == -2)
						Toast.makeText(mContext, R.string.connecterror, Toast.LENGTH_SHORT).show();
					else if (rs == -1)
						Toast.makeText(mContext, R.string.servererror, Toast.LENGTH_SHORT).show();
					else if (rs == 0)
						Toast.makeText(mContext, R.string.datainputerror, Toast.LENGTH_SHORT).show();
					else {
						Toast.makeText(mContext, R.string.updateshippingsuccess, Toast.LENGTH_SHORT).show();
						mDialog.dismiss();
					}
				};
			}.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
		}
	}

}
